package com.simple.piger.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.simple.piger.dto.req.InviteCompanyReqDto;
import com.simple.piger.enums.InviteCompanyScaleEnums;
import com.simple.piger.utils.DateUtils;
import com.simple.piger.utils.IdUtil;
import com.simple.piger.utils.PageUtil;
import com.simple.piger.dto.resp.company.InviteCompanyRespDto;
import com.simple.piger.entity.InviteCompany;
import com.simple.piger.mapper.InviteCompanyMapper;
import com.simple.piger.service.InviteCompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公司表 服务实现类
 * </p>
 *
 * @author baolw
 * @since 2020-02-06
 */
@Service
public class InviteCompanyServiceImpl extends ServiceImpl<InviteCompanyMapper, InviteCompany> implements InviteCompanyService {

    @Override
    public List<InviteCompanyRespDto> list(Integer page, Integer limit, String companyName) {
        List<InviteCompanyRespDto> list = this.baseMapper.list(PageUtil.getPageStart(page, limit), limit, companyName);
        for (InviteCompanyRespDto dto : list) {
            dto.setScaleValue(InviteCompanyScaleEnums.transfer(dto.getScale()));
            dto.setFinancingValue(InviteCompanyScaleEnums.transfer(dto.getFinancing()));
            dto.setCreateDate(DateUtils.dateFormat(dto.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        return list;
    }

    @Override
    public int count(String companyName) {
        return this.baseMapper.count(companyName);
    }

    @Override
    public void saveOrUpdate(InviteCompanyReqDto reqDto) {
        if (StringUtils.isNotBlank(reqDto.getId())) {
            InviteCompany inviteCompany = this.selectById(reqDto.getId());
            BeanUtils.copyProperties(reqDto, inviteCompany);
            inviteCompany.setProvinceCode(reqDto.getProvenceCode());
            inviteCompany.setCityCode(reqDto.getCityCode());
            inviteCompany.updateById();
        } else {
            InviteCompany inviteCompany = new InviteCompany();
            BeanUtils.copyProperties(reqDto, inviteCompany);
            inviteCompany.setId(IdUtil.getId());
            inviteCompany.setCreateTime(new Date());
            inviteCompany.setUpdateTime(new Date());
            inviteCompany.setDeleteFlag("0");
            inviteCompany.setLat(reqDto.getLat());
            inviteCompany.setLng(reqDto.getLng());
            inviteCompany.setProvinceCode(reqDto.getProvenceCode());
            inviteCompany.setCityCode(reqDto.getCityCode());
            inviteCompany.insert();
        }
    }
}
